// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
	async subjectlistAction(){
		let page = this.post();
		let pages = page.pages;
		let limits = page.limits;
		let search = page.search;
		let zt = page.zt;
		let shopid = this.shop_userinfo.departmentid;

		let data = await this.model('or_shop_subject')
				.alias('ss')
				.field('ss.*, s.name, s.number, sc.name as cname, b.name as bname, s.remarks, s.isdisable as sisdisable')
				.join('think_or_subject s on s.id=ss.subjectid')
				.join('think_or_brand b on b.id=s.brandid')
				.join('think_or_subjectcategory sc on sc.id=s.categoryid')
				.where({'ss.shopid':shopid,'ss.isdisable':zt, 's.name|s.number':['like','%'+search+'%']})
				.page(pages, limits)
				.order('ss.id DESC')
				.countSelect();

		return this.json(data);
	}

	// pull organization subjects to shop
	async pullsubjectsAction(){

		let or_subsid = JSON.parse(this.post('data'));
		let shopid = this.shop_userinfo.departmentid;
		// check pull subjects whether repeat in shop.
		let pulled_subs = await this.model('or_shop_subject').where({shopid: shopid}).getField('subjectid');

		pulled_subs.forEach((v)=>{
			if (or_subsid.indexOf(v) !== -1) return this.fail('拉取的项目已经存在');
		})

		let or_sub_list = await this.model('or_subject').where({id: ['IN', or_subsid]}).select();

		var add_data = [];

		or_sub_list.forEach((v)=>{
			add_data.push({
				subjectid: v.id,
				shopid: shopid,
				price: v.price,
				usetime: v.usetime,
				xstdtype: v.xstdtype,
				xspercent: v.xspercent,
				xsfixed: v.xsfixed,
				sgtdtype: v.sgtdtype,
				sgpercent: v.sgpercent,
				sgfixed: v.sgfixed,
				khtdtype: v.khtdtype,
				khpercent: v.khpercent,
				khfixed: v.khfixed
			})
		})

		//写入数据库
		let res = await this.model('or_shop_subject').addMany(add_data);

		return res?this.success("拉取成功"):this.fail('拉取失败');
	}

	//编辑项目
	async editsubjectAction(){
		
		let sinfo = this.post();

		let data = {
			price: sinfo.price,
			usetime: sinfo.usetime,
			xstdtype: sinfo.xstdtype,
			xspercent: Number(sinfo.xspercent/100).toFixed(2),
			xsfixed: sinfo.xsfixed,
			sgtdtype: sinfo.sgtdtype,
			sgpercent: Number(sinfo.sgpercent/100).toFixed(2),
			sgfixed: sinfo.sgfixed,
			khtdtype: sinfo.khtdtype,
			khpercent: Number(sinfo.khpercent/100).toFixed(2),
			khfixed: sinfo.khfixed
		}

		let res = await this.model('or_shop_subject').where({id: sinfo.id}).update(data);

		return res?this.success('更改成功'):this.fail('更改失败');

	}

	// 项目启用和停用
	async unstopAction(){
		let id = this.post('id');
		let status = this.post('status');

		let res = await this.model('or_shop_subject').where({id:id}).update({isdisable:status});
		
		if (status*1) {
			return res?this.success('停用成功'):this.fail('停用失败');
		} else {
			return res?this.success('启用成功'):this.fail('启用失败');
		}
	}

	// 获取机构项目二级分类
	async getorsubjectcategoryAction(){

		let shopid = this.shop_userinfo.departmentid;

		let orid = await this.model('or_department').where({id: shopid}).getField('pid',true);

		// let data = await this.model('or_subjectcategory').where({departmentid: orid, pid: ['>', 0]}).select();

		let data = await this.model().query('select a.id,CONCAT_WS(" ---> ",a.name,b.name) as name FROM `think_or_subjectcategory` AS a LEFT JOIN `think_or_subjectcategory` AS b ON a.pid = b.id WHERE a.departmentid='+orid+' AND a.pid > 0');

		return this.json(data);
	}

	// 获取机构分类下的项目
	async querysubjectsAction(){

		let cateids = JSON.parse(this.post('cates'));

		if (think.isEmpty(cateids)) cateids = [0];

		let shopid = this.shop_userinfo.departmentid;

		let orid = await this.model('or_department').where({id: shopid}).getField('pid',true);

		let subjectlist = await this.model('or_subject').where({departmentid: orid, categoryid: ['IN', cateids], isdisable:0}).select();

		let existid = await this.model('or_shop_subject').where({shopid: shopid}).getField('subjectid');

		let data = {
			subjects : subjectlist,
			existsub : existid
		}

		return this.json(data);
	}

}